#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@File    :   network_config_inspector.py
@Time    :   2025-07-16 08:21:51
@Author  :   Li Rui
@Version :   1.0
@Contact :   marjey_lee@163.com
@Desc    :   None
"""


import ll
import ll.llm as llm

_SYSTEM_MESSAGE = """
你是一个网络配置检查助手，你的任务是检查网络配置和运行参数文件的语法错误。
我给你的样例如下：

        【IP】:172.17.8.12
        【设备名称】:FD_YNKM_A_SRV_SW04$
        【是否执行成功】:True
        【执行命令】:display memory
        【命令类型】:当前内存
        【解析内容】:
        【错误日志】:
        【执行结果】:<FD_YNKM_A_SRV_SW04>display memory
 Memory utilization statistics at 2025-07-15 09:01:22+08:00
 System Total Memory Is: 361375708 bytes
 Total Memory Used Is: 170665096 bytes
 Memory Using Percentage Is: 47%<FD_YNKM_A_SRV_SW04>

 你需要对上述进行json格式化,返回给我解析过标准化的json。如下：
 ```json
 {
    "ip": "172.17.8.12",
    "device_name": "FD_YNKM_A_SRV_SW04$",
    "is_success": "True",
    "command": "display memory",
    "command_type": "当前内存",
    "parsed_content": {
        "system_total_memory": "361375708 bytes",
        "total_memory_used": "170665096 bytes",
        "memory_using_percentage": "47%",
        },
    "find_error": False, # 如果 【执行结果】 异常则为True
    "error_message":""

 }
 ```
"""


class NetworkConfigInspector:
    def __init__(self, llm_client_name):
        self.llm_client_name = llm_client_name
        self.llm_client = llm.ClientsFactory.create_chat_client(llm_client_name)
        self.llm_client.set_system_message(_SYSTEM_MESSAGE)

    @staticmethod
    def create_default_inspector():
        from app.cli_inspect_server.config.llm_config import (
            NETWORK_CONFIG_INSPECTOR_CLIENT_NAME,
        )

        return NetworkConfigInspector(NETWORK_CONFIG_INSPECTOR_CLIENT_NAME)

    def __call__(self, message):
        try:
            response = self.llm_client.chat(message)
            content = llm.stream2str(response)
            json_resp = llm.parse_json_output(content)
            return ll.json2str(json_resp)
        except Exception as e:
            ll.error(f"NetworkConfigInspector error: {e}")
            return str(e)
